<!-- eslint-disable vue/no-use-v-if-with-v-for -->
<template>
  <div class="container">
    <!-- 组题列表 -->
    <div class="bgc">
      <!-- form表单  搜索 -->
      <div style="margin-bottom: 10px">
        <span>关键字:</span>
        <el-input
          v-model="name"
          class="inp"
          placeholder="请输入内容"
          clearable
        />
        <el-button style="float: right" @click="qingchu">清除</el-button>
        <el-button type="primary" style="float: right" @click="sousuo">
          搜索
        </el-button>
      </div>
    </div>
    <div class="bgc" style="margin: 10px 0; background-color: #e2dfdf">
      数据一共 {{ total }} 条
    </div>
    <div class="bgc">
      <el-table
        :data="tableData"
        style="width: 100%; margin-bottom: 20px"
        :header-row-style="{ 'background-color': '#000' }"
      >
        <el-table-column prop="number" label="编号" width="120" />
        <el-table-column prop="questionType" label="题型" width="120">
          <template v-slot="{ row }">
            {{
              row.questionType === '1'
                ? '单选'
                : row.questionType === '2'
                  ? '多选'
                  : '简单'
            }}
          </template>
        </el-table-column>
        <el-table-column prop="number" label="题目编号" width="120">
          <template v-slot="{ row }">
            <button class="butt" @click="viewFn(row.id)">
              {{ row.number }}
            </button>
          </template>
        </el-table-column>
        <el-table-column prop="catalog" label="目录" width="120" />
        <el-table-column prop="addDate" label="录入时间">
          <template v-slot="{ row }">
            {{ row.addDate | time }}
          </template>
        </el-table-column>
        <el-table-column label="答题时间(s)">
          <template>
            {{ 800 }}
          </template>
        </el-table-column>
        <el-table-column label="正确率">
          <template>
            <span> 80% </span>
          </template>
        </el-table-column>

        <el-table-column prop="creator" label="录入人" />
        <el-table-column prop="address" label="操作" width="240">
          <template v-slot="{ row }">
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              plain
              @click="delRand(row.id)"
            />
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          background
          :page-sizes="[10, 5, 2, 1]"
          :page-size="formInline.pagesize"
          layout="->, prev,pager,next,sizes,jumper"
          :current-page="formInline.page"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <!-- 组件 -->
    <preview :show-dialog.sync="showDialog" :question-id="questionId" />
  </div>
</template>
<script>
import { getBankListApi, delRandomsApi } from '@/api/questionBank'
import {
  getSubjectsListApi,
  getDirectoryListApi,
  getTagsListApi
} from '@/api/subject'
import preview from './preview.vue'
import { getUserListSimpleApi } from '@/api/backstage'
// import {  } from '@api/questionBank'
export default {
  components: { preview },
  data() {
    return {
      name: '',
      // 搜索表单
      formInline: {
        page: 1, // 当前页数
        pagesize: 10, // 页尺寸
        subjectID: null, // 学科
        catalogID: null, // 目录
        tags: '', // 标签名称
        keyword: '', // 	关键字
        questionType: '', // 试题类型
        difficulty: '', // 难度
        direction: '', // 方向
        creatorID: null, // 录入人
        remarks: '', // 题目备注
        shortName: '', // 企业简称
        province: '', // 企业所在地省份
        city: '' // 企业所在城市
      },
      total: 0, // 数据总条数
      tableData: [],
      SubjectsId: null, // 学科id
      SubjectsList: [], // 学科列表
      DirectoryList: [], // 目录列表
      TagsList: [], // 标签列表
      // 试题类型
      typesOf: [
        { value: 1, label: '单选' },
        { value: 2, label: '多选' },
        { value: 3, label: '简单' }
      ],
      // 难度
      difficulty: [
        { value: 1, label: '简单' },
        { value: 2, label: '一般' },
        { value: 3, label: '困难' }
      ],
      // 方向
      directionlist: [
        { name: 'o2o' },
        { name: '外包服务' },
        { name: '企业服务' },
        { name: '互联网金融' },
        { name: '企业咨询' },
        { name: '互联网' },
        { name: '电子商务' },
        { name: '其他' }
      ],
      // 录入人
      enterlist: [],
      questionId: null, // 试题id
      showDialog: false // 组件状态
    }
  },
  async created() {
    const { data } = await getSubjectsListApi()
    this.SubjectsList = data
    const user = await getUserListSimpleApi()
    this.enterlist = user.data
    this.getBankList()
  },
  methods: {
    // 清除
    qingchu() {
      this.name = ''
      this.getBankList()
    }, // 搜索
    async sousuo() {
      if (this.name !== '') {
        const { data } = await getSubjectsListApi({
          page: this.page,
          pagesize: this.pagesize,
          keyword: this.name
        })
        this.numberOfArticles = data.counts
        this.tableData = data.items
      } else {
        this.getBankList()
      }
    },
    // 基础题库列表方法
    async getBankList() {
      const { data } = await getBankListApi(this.formInline)
      this.tableData = data.items
      this.total = data.counts
      console.log(data)
    },
    // 页码发生变化
    handleSizeChange(val) {
      this.formInline.pagesize = val
      this.getBankList()
    },
    handleCurrentChange(val) {
      this.formInline.page = val
      this.getBankList()
    },
    // 清空搜索框
    closeFn() {
      this.formInline = {
        page: 1, // 当前页数
        pagesize: 10, // 页尺寸
        subjectID: null, // 学科
        catalogID: null, // 目录
        tags: '', // 标签名称
        keyword: '', // 	关键字
        questionType: '', // 试题类型
        difficulty: '', // 难度
        direction: '', // 方向
        creatorID: null, // 录入人
        remarks: '', // 题目备注
        shortName: '', // 企业简称
        province: '', // 企业所在地省份
        city: '' // 企业所在城市
      }
      this.getBankList()
    },

    // 删除弹出框
    delRand(id) {
      this.$confirm('此操作将删除该题目, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await delRandomsApi(id)
          this.getBankList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 学科选中
    async SubjectsFn(id) {
      this.SubjectsId = id
      const { data } = await getDirectoryListApi(id)
      this.DirectoryList = data.items
      const tags = await getTagsListApi(id)
      this.TagsList = tags.data.items
      // console.log(tags.data.items)
    },
    // 查看
    viewFn(id) {
      this.questionId = id
      this.showDialog = true
    }
  }
}
</script>
<style scoped lang="scss">
.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.el-col {
  border-radius: 4px;
}
.bg-purple-dark {
  background: #99a9bf;
}
.bg-purple {
  background: #d3dce6;
}
.bg-purple-light {
  background: #e5e9f2;
}
.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
.bgc {
  background-color: #fff;
  padding: 10px;
  border-radius: 10px;
}
.top {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
  .illustrate {
    color: rgb(221, 104, 104);
    font-size: 12px;
  }
}
.el-select {
  width: 100%;
}
.inp {
  width: 200px;
  margin: 0 10px;
}
.butt {
  color: #1890ff;
  background-color: transparent;
  border: 0;
  cursor: pointer;
}
</style>
